package com.jeesite.modules.kj.utils;

import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.io.PropertiesUtils;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.utils.SpringUtils;
import com.jeesite.modules.kj.enums.IntegralEnum;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.entity.KjMemberIntegralHistory;
import com.jeesite.modules.kj.entity.KjMemberIntegrals;
import com.jeesite.modules.kj.exception.LowIntegralException;
import com.jeesite.modules.kj.service.KjMemberIntegralHistoryService;
import com.jeesite.modules.kj.service.KjMemberIntegralsService;
import com.jeesite.modules.kj.service.KjMemberService;
import com.jeesite.modules.wx.TemplateMsg.TemplateDataObj;
import com.jeesite.modules.wx.TemplateMsg.WxTemplateMsgUtils;

import java.math.BigDecimal;
import java.util.List;

public class IntegralUtil {
    /**
     * 静态内部类，延迟加载，懒汉式，线程安全的单例模式
     */
    private static final class Static {
        private static KjMemberService kjMemberService = SpringUtils.getBean(KjMemberService.class);
        private static KjMemberIntegralsService kjMemberIntegralsService = SpringUtils.getBean(KjMemberIntegralsService.class);
        private static KjMemberIntegralHistoryService kjMemberIntegralHistoryService = SpringUtils.getBean(KjMemberIntegralHistoryService.class);
    }


    public static void addIntegrals(KjMember kjMember, String relationId, String jobTypeId, IntegralEnum integralEnum, BigDecimal integrals, BigDecimal hours, String remarks){
        KjMemberIntegralHistory history = new KjMemberIntegralHistory();
        history.setIntegralType(integralEnum.getType());
        history.setIntegral(integrals);
        history.setKjMember(kjMember);
        history.setRemarks(remarks);
        history.setHours(hours);
        history.setRelationId(relationId);
        history.setJobTypeId(jobTypeId);
        history.setIntegralTypeName(integralEnum.getName());
        Static.kjMemberIntegralHistoryService.save(history);
        //勋章统计合计
        BadgeUtil.gainBadge(kjMember, jobTypeId);
        Static.kjMemberIntegralsService.addIntegrals(kjMember.getId(), integrals, hours);


        if (kjMember != null && kjMember.getWxopenid() != null) {
            PropertiesUtils props = PropertiesUtils.getInstance();
            String url = props.getProperty("wx.service.redirectHost") + "/Integral";
            List<TemplateDataObj> templateDataObjs = ListUtils.newArrayList();
            templateDataObjs.add(new TemplateDataObj("date1", DateUtils.getDateTime()));
            templateDataObjs.add(new TemplateDataObj("number2", integrals.toString() + "积分"));
            templateDataObjs.add(new TemplateDataObj("thing3", remarks));
//            WxTemplateMsgUtils.sendByTemplate(props.getProperty("wx.template.integralChange"), kjMember.getWxopenid(), "", templateDataObjs);


        }
    }

    public static void subtractIntegrals(KjMember kjMember, String relationId, IntegralEnum integralEnum, BigDecimal integrals, String remarks) throws LowIntegralException {
        Static.kjMemberIntegralsService.subtractIntegrals(kjMember.getId(), integrals);
        KjMemberIntegralHistory history = new KjMemberIntegralHistory();
        history.setIntegralType(integralEnum.getType());
        history.setIntegral(integrals.negate());
        history.setKjMember(kjMember);
        history.setRemarks(remarks);
        history.setRelationId(relationId);
        history.setIntegralTypeName(integralEnum.getName());
        Static.kjMemberIntegralHistoryService.save(history);

    }



    public static BigDecimal getMemberJobIntegrals(KjMember kjMember, String jobTypeId){
        KjMemberIntegralHistory history = new KjMemberIntegralHistory();
        history.setJobTypeId(jobTypeId);
        history.setKjMember(kjMember);
        KjMemberIntegrals integrals = Static.kjMemberIntegralsService.findByMemberId(kjMember.getId());
        return integrals.getTotalIntegrals();
    }

    public static BigDecimal getMemberIntegrals(KjMember kjMember){
        KjMemberIntegrals integrals = Static.kjMemberIntegralsService.findByMemberId(kjMember.getId());
        return integrals.getTotalIntegrals();
    }


    public static BigDecimal getMemberHours(KjMember kjMember){
        KjMemberIntegrals integrals = Static.kjMemberIntegralsService.findByMemberId(kjMember.getId());
        return integrals.getTotalHours();
    }

}
